import store from '@/store'

/**
 * 检查是否拥有value访问权限
 * @param value 待检查的权限
 * @returns {boolean} 是否有权限
 */
export function checkPermission (value) {
  if (value && value.length > 0) {
    const roles = store.getters && store.getters.permissions
    const hasPermission = roles.indexOf(value) > -1
    // console.log('store.getters', store.getters)
    // console.log('store.getters.permissions', store.getters.permissions)
    // console.log('roles', roles)
    // console.log('hasPermission', hasPermission)
    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error('无权')
    return false
  }
}

/**
 * 本地可用权限
 */
export function permission () {
  return {
    permissionGet: '/permission:get',
    permissionDelete: '/permission:delete',
    permissionPut: '/permission:put',
    permissionTreeGet: '/permission:tree:get',
    roleListGet: '/role/list:get',
    roleOptionGet: '/role/option:get',
    roleUpdatePost: '/role/update:post',
    roleInfoGet: '/role/info:get',
    roleInsertPost: '/role/insert:post',
    roleDelete: '/role/delete',
    roleReset: '/role/reset',
    roleBindUserPost: '/role/bind/user:post',
    roleBindUserDelete: '/role/bind/user:delete',
    roleBindUserNameGet: '/role/bind/user/name:get',
    roleBindUserGet: '/role/bind/user:get',
    userList: '/user/list',
    userDelete: '/user:delete',
    userEnable: '/user/enable',
    userInsert: '/user/insert'
  }
}
